package com.stu.chart.controller;

import com.stu.chart.service.StaffService;
import com.stu.chart.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;


/**
 * @author dsj
 */
@Controller
public class LoginController {
    @Autowired
    StaffService loginservice;

    @RequestMapping("/loginvalidate")
    public String loginvalidate(@RequestParam("username") String username, @RequestParam("password") String pwd, HttpSession httpSession) {
        if (username == null) {
            return "login";
        }
        String realpwd = loginservice.getpwdbyname(username);
        if (realpwd != null && pwd.equals(realpwd)) {
            long uid = loginservice.getUidbyname(username);
            httpSession.setAttribute("uid", uid);
            return "chatroom";
        } else {
            return "fail";
        }
    }
    @RequestMapping("/")
    public String login1() {
        User user = SecurityUtil.getUser();
        if(user != null){
            return "chatroom";
        }else{
            return "login1";
        }
    }
    @RequestMapping("/login")
    public String login() {
        return "login1";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/search")
    public String search() {
        return "search";
    }

    @RequestMapping("/info")
    public String info() {
        return "info";
    }

    @RequestMapping("/admin")
    public String admin() {
        return "admin";
    }

    @RequestMapping("/master")
    public String master() {
        return "master";
    }

    @RequestMapping("/index")
    public String chatroom() {
        System.out.println("jinlail");
        return "chatroom";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession httpSession) {
        httpSession.invalidate();
        return "login";
    }
}
